package com.work.yshop.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsControllerCompat;

import com.work.yshop.R;
import com.work.yshop.mvp.contract.LoginContract;
import com.work.yshop.utils.UserSession;

/**
 * 发布选择页面
 * 用户可以在此页面选择发布商品、文章等内容
 * 买家可以进入页面，但不能发布商品
 */
public class PublishActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView btnBack;
    private LinearLayout publishProduct;
    private LinearLayout publishPurchase;
    private LinearLayout publishVideo;
    private LinearLayout publishArticle;
    private LinearLayout publishLive;
    private LinearLayout publishAnswer;
    private TextView tvUserTypeHint;
    private View statusBarPlaceholder;

    // 获取当前用户类型
    private int userType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish);

        // 设置状态栏
        setupStatusBar();

        // 获取用户类型
        userType = UserSession.getInstance().getUserType();

        initViews();
        setupListeners();
        updateUIBasedOnUserType();
    }

    /**
     * 设置状态栏
     */
    private void setupStatusBar() {
        // 让内容区域延伸到状态栏
        WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
        
        // 设置状态栏图标为深色
        WindowInsetsControllerCompat controller = new WindowInsetsControllerCompat(getWindow(), getWindow().getDecorView());
        controller.setAppearanceLightStatusBars(true); // 状态栏文字和图标为黑色
        
        // 设置状态栏颜色为透明
        getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
        
        // 设置状态栏占位View的高度
        statusBarPlaceholder = findViewById(R.id.status_bar_placeholder);
        if (statusBarPlaceholder != null) {
            int statusBarHeight = getStatusBarHeight();
            ViewGroup.LayoutParams layoutParams = statusBarPlaceholder.getLayoutParams();
            layoutParams.height = statusBarHeight;
            statusBarPlaceholder.setLayoutParams(layoutParams);
        }
    }
    
    /**
     * 获取状态栏高度
     */
    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    private void initViews() {
        btnBack = findViewById(R.id.btn_back);
        publishProduct = findViewById(R.id.publish_product);
        publishPurchase = findViewById(R.id.publish_purchase);
        publishVideo = findViewById(R.id.publish_video);
        publishArticle = findViewById(R.id.publish_article);
        publishLive = findViewById(R.id.publish_live);
        publishAnswer = findViewById(R.id.publish_answer);
    }

    private void setupListeners() {
        btnBack.setOnClickListener(this);
        publishProduct.setOnClickListener(this);
        publishPurchase.setOnClickListener(this);
        publishVideo.setOnClickListener(this);
        publishArticle.setOnClickListener(this);
        publishLive.setOnClickListener(this);
        publishAnswer.setOnClickListener(this);
    }
    
    /**
     * 根据用户类型更新UI
     */
    private void updateUIBasedOnUserType() {
        if (userType == LoginContract.USER_TYPE_BUYER) {
            // 如果是买家，显示提示信息
            Toast.makeText(this, "买家账号可以浏览但无法发布商品", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        
        if (id == R.id.btn_back) {
            onBackPressed();
        } else if (id == R.id.publish_product) {
            // 发布商品 - 只有卖家才能发布
            if (userType == LoginContract.USER_TYPE_SELLER) {
                Intent intent = new Intent(this, PublishProductActivity.class);
                startActivity(intent);
            } else {
                Toast.makeText(this, "只有卖家才能发布商品", Toast.LENGTH_SHORT).show();
            }
        } else if (id == R.id.publish_article) {
            // 发布图文 - 只有卖家才能发布
            if (userType == LoginContract.USER_TYPE_SELLER) {
                Intent intent = new Intent(this, AddArticleActivity.class);
                startActivity(intent);
            } else {
                Toast.makeText(this, "只有卖家才能发布文章", Toast.LENGTH_SHORT).show();
            }
        } else {
            // 其他功能暂未实现
            Toast.makeText(this, "该功能暂未开放，敬请期待", Toast.LENGTH_SHORT).show();
        }
    }
} 